package com.j2eefast.bussiness.contract.utils;

import com.j2eefast.bussiness.contract.entity.BusDepreciationEntity;
import com.j2eefast.bussiness.contract.entity.BusEquipmentEntity;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xjt
 * @version 1.0
 */
public class ReadBusEquipmentExcelUtil {
    //总行数
    private static int totalRows = 0;
    //总条数
    private static int totalCells = 0;
    //错误信息接收器
    private static String errorMsg;

    /**
     * 读EXCEL文件，获取信息集合
     * @return
     */
    public static List<BusEquipmentEntity> getExcelInfo(MultipartFile mFile, String pathName) {
        String fileName = mFile.getOriginalFilename();//获取文件名
        try {
            if (!validateExcel(fileName)) {// 验证文件名是否合格
                return null;
            }
            boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
            if (isExcel2007(fileName)) {
                isExcel2003 = false;
            }
            FileInputStream   is = new FileInputStream(pathName);//定义文本输入流
            List<BusEquipmentEntity> userList = createExcel(is, isExcel2003);
            return userList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据excel里面的内容读取客户信息
     * @param is 输入流
     * @param isExcel2003 excel是2003还是2007版本
     * @return
     * @throws IOException
     */
    public static List<BusEquipmentEntity> createExcel(InputStream is, boolean isExcel2003) {
        try{
//            Workbook wb = null;
//            if (isExcel2003) {// 当excel是2003时,创建excel2003
//                wb = new HSSFWorkbook(is);
//            } else {// 当excel是2007时,创建excel2007
//                wb = new XSSFWorkbook(is);
//            }
            Workbook wb = null;
            try {
                wb = WorkbookFactory.create(is);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
            List<BusEquipmentEntity> userList = readExcelValue(wb);// 读取Excel里面客户的信息
            return userList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 读取Excel里面客户的信息
     * @param wb
     * @return
     */
    private static List<BusEquipmentEntity> readExcelValue(Workbook wb) {
        //默认会跳过第一行标题
        // 得到第一个shell
        Sheet sheet = wb.getSheetAt(0);
        // 得到Excel的行数
        totalRows = sheet.getPhysicalNumberOfRows();
        // 得到Excel的列数(前提是有行数)
        if (totalRows > 1 && sheet.getRow(0) != null) {
            totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        List<BusEquipmentEntity> userList = new ArrayList<BusEquipmentEntity>();
        // 循环Excel行数
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            BusEquipmentEntity user = new BusEquipmentEntity();
            // 循环Excel的列
            for (int c = 0; c < totalCells; c++) {
                Cell cell = row.getCell(c);
                if (null != cell) {
                    if (c == 0) {           //第一列
                        //如果是纯数字,将单元格类型转为String
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setEquipmentCode(cell.getStringCellValue());//将单元格数据赋值给user  设备编码equipmentCode
                    }
                    else if (c == 1){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setEquipmentName(cell.getStringCellValue());//将单元格数据赋值给user  设备名称equipmentName
                    }
                    else if (c == 2){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setMeterageCompany(cell.getStringCellValue());//将单元格数据赋值给user  计量公司meterageCompany
                    }
                    else if (c == 3){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setCompanyCode(cell.getStringCellValue());//将单元格数据赋值给user  计量公司编号companyCode
                    }
                    else if (c == 4){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
//                        Date test=parseTime(cell.getStringCellValue());
//                        user.setMeterageTime(test);//将单元格数据赋值给user  计量时间meterageTime
                    }
                    else if (c == 5){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setMeteringUnit(cell.getStringCellValue());//将单元格数据赋值给user  单位meteringUnit
                    }
                    else if (c == 6){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setEquipmentModel(cell.getStringCellValue());//将单元格数据赋值给user  设备型号 equipmentModel
                    }
                    else if (c == 7){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setMeasuringRange(cell.getStringCellValue());//将单元格数据赋值给user  测量范围measuringRange
                    }
                    else if (c == 8){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setUsages(cell.getStringCellValue());//将单元格数据赋值给user  使用情况usages
                    }
                    else if (c == 9){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setUseDepartment(cell.getStringCellValue());//将单元格数据赋值给user  使用部门useDepartment
                    }
                    else if (c == 10){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setSafekeeping(cell.getStringCellValue());//将单元格数据赋值给user  保管人员safekeeping
                    }
                    else if (c == 11){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setDepositAddr(cell.getStringCellValue());//将单元格数据赋值给user  存放地点depositAddr
                    }
                    else if (c == 12){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setEquipmentValue(cell.getStringCellValue());//将单元格数据赋值给user  设备原值equipmentValue
                    }
                    else if (c == 13){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setManufacturer(cell.getStringCellValue());//将单元格数据赋值给user  制造厂家manufacturer
                    }
                    else if (c == 14){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setStandardCode(cell.getStringCellValue());//将单元格数据赋值给user  国标编号standardCode
                    }
                    else if (c == 15){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setConfigure(cell.getStringCellValue());//将单元格数据赋值给user  详细配置configure
                    }
                    else if (c == 16){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
//                        Date test=parseTime(cell.getStringCellValue());
//                        user.setPurchaseTime(test);//将单元格数据赋值给user  购买日期purchaseTime
                    }
                    else if (c == 17){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
//                        user.setProductionTime(cell.getStringCellValue());//将单元格数据赋值给user  出厂日期productionTime
                    }
                    else if (c == 18){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
//                        user.setPutTime(cell.getStringCellValue());//将单元格数据赋值给user  入账日期putTime

                    }
                    else if (c == 19){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setIncrease(cell.getStringCellValue());//将单元格数据赋值给user  增加方式increase
                    }
                    else if (c == 20){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setDepreciation(cell.getStringCellValue());//将单元格数据赋值给user  折旧方法depreciation
                    }
                    else if (c == 21){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setJingcanValue(cell.getStringCellValue());//将单元格数据赋值给user  净残值率jingcanValue
                    }
                    else if (c == 22){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setAge(cell.getStringCellValue());//将单元格数据赋值给user  使用年限age
                    }
                    else if (c == 23){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setAccumulatedDepreciation(cell.getStringCellValue());//将单元格数据赋值给user  累计折旧accumulatedDepreciation
                    }
                    else if (c == 24){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setWorth(cell.getStringCellValue());//将单元格数据赋值给user  净值worth
                    }
                    else if (c == 25){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setClassifyCode(cell.getStringCellValue());//将单元格数据赋值给user  分类编码classifyCode
                    }
                    else if (c == 26){
                        if(cell.getCellTypeEnum()  == CellType.NUMERIC){
                            cell.setCellType(CellType.STRING);
                        }
                        System.out.println(cell.getStringCellValue());
                        user.setEquipmentType(cell.getStringCellValue());//将单元格数据赋值给user  分类名称equipmentType
                    }

                }
                userList.add(user);
            }
            // 添加到list

        }
        return userList;
    }
    /**
     * 验证EXCEL文件
     *
     * @param filePath
     * @return
     */
    public static boolean validateExcel(String filePath) {
        if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
            errorMsg = "文件名不是excel格式";
            return false;
        }
        return true;
    }

    public static Date parseTime(String date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse =null;
        try {
            parse = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  parse;
    }

    // @描述：是否是2003的excel，返回true是2003
    public static boolean isExcel2003(String filePath)  {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }
    //@描述：是否是2007的excel，返回true是2007
    public static boolean isExcel2007(String filePath)  {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}
